<?php
require_once('Zend/Pdf.php');

class Gorilla_Pdf extends Zend_Pdf {
    protected $_activePage = false;
    protected $_pageSize = Zend_Pdf_Page::SIZE_LETTER;
    protected $_font = false;

    protected $_margin;

	protected $_header;
	protected $_footer;

	protected $_options;
	protected $_defaultOptions = array(
		'margin' => array( 
			'left' => 20,
			'top' => 20,
			'right' => 20,
			'bottom' => 20
		),
		'align' => 'left',
		'font' => Zend_Pdf_Font::FONT_HELVETICA
	);
	
    public function __construct($source = null, $revision = null, $load = null) {
        parent::__construct($source, $revision, $load);
        
        $this->init();
    }

	public function init() {
        
    }
    
	public function setOptions(array $options) {
 		$diff = array_diff_key($options, $this->_defaultOptions);
        if ($diff) {
            list($key, $val) = each($diff);
            require_once 'Gorilla/Pdf/Exception.php';
            throw new Gorilla_Pdf_Exception("Unknown Gorilla_Pdf option: $key");
        }

        foreach ($this->_defaultOptions as $key => $val) {
            if (!array_key_exists($key, $options)) {
                $options[$key] = $val;
            }
        }

        $this->_options = $options;
	}

	public function getOptions() {
		return $this->_options;
	}
	
	public function getOption($key) {
		return $this->_options[ $key ];
	}
	
    /**
     * Sets the page size of the next page created. Any pages before it will not
     * be set to the size.
     * @param Zend_Pdf_Page::constants $pageSize
     */
    public function setPageSize($pageSize) {
        $this->_pageSize = $pageSize;
    }

    public function setFont($font, $size) {
        $this->_font = Zend_Pdf_Font::fontWithName($font);
        $this->_activePage->setFont($this->_font, $size);
    }

    /**
     * Returns the current page the pdf is using to processing
     * @return Zend_Pdf_Page
     */
    public function getActivePage() {
        if($this->_activePage === false && empty($this->pages)) {
            $this->newPage();
            $this->_activePage = &end($this->pages);
        }
        return $this->_activePage;
    }

    public function newPage($className = 'Gorilla_Pdf_Page');
    
        $page = new $className($this->_pageSize);
    
    	if ($page instanceof Gorilla_Pdf_Page) {
	        $this->pages [] = $page;
	        $page->setDocument($this);
	        return $page;    	
    	}
    	
    	require_once 'Gorilla/Pdf/Exception.php';
		throw new Gorilla_Pdf_Exception('page object must be instance of Gorilla_Pdf_Page');
		
    }

	/*
	 
	---  Methods bellow should be only available in Gorilla_Pdf_Page class ----
	 

    public function addX($x) {
        return $this;
    }

    public function addY($y) {
        return $this;
    }

    public function drawText($text) {
        return $this;
    }

    public function drawHline($x) {
        return $this;
    }

    public function setLineHeight($pixels) {
        return $this;
    }
    
    public function setX($x) {
        return $this;
    }

    public function setY($y) {
        return $this;
    }

    public function setAlign($align) {
        return $this;
    }

    public function setWrap($width) {
        return $this;
    }
    
    ----
    
    */

    /**
     * Set header
     * @param Gorilla_Pdf_Block $header  
     * @return Gorilla_Pdf
     */	
    public function setHeader(Gorilla_Pdf_Block $header) {
    	$this->_header = $header;
        return $this;
    }

    /**
     * Set footer
     * @param Gorilla_Pdf_Block $footer 
     * @return Gorilla_Pdf
     */	
    public function setFooter(Gorilla_Pdf_Block $footer) {
    	$this->_footer = $footer;
        return $this;
    }

    /**
     * Set document margin
     * @param int $left
     * @param int $top
     * @param int $right
     * @param int $bottom     
     * @return Gorilla_Pdf
     */
    public function setMargin($left, $top = null, $right = null, $bottom = null) {
    
    	$this->_margin = new stdClass;
    	
    	$this->_margin->left = $left;
    	$this->_margin->top = isset($top)?$top:$this->_margin->left;
    	
    	$this->_margin->right = isset($right)?$right:$this->_margin->left;
    	$this->_margin->bottom = isset($bottom)?$bottom:$this->_margin->right;
    	
    	
        return $this;
    }


}
